$(document).ready(function(){
	// Init new banner dialog
	$("#newBannerDialog").dialog({
		autoOpen: false,
		width: 560,
		resizable: false,
		buttons: {
			"Thêm": function(){
				$("#newBannerErrorRow").css("display", "none");
				$.post("/banner/add", $("#newBannerForm").serialize(), function(data){
					var returnData = $.parseJSON(data);
					if (returnData.code == 0)
					{
						window.location.reload();
						$("#newBannerDialog").dialog("close");
					}
					else
					{
						$("#newBannerErrorRow").css("display", "");
						$("#newBannerErrorMessage").html(returnData.data);
					}
				});
				return false;
			},
			"Đóng": function(){
				$(this).dialog("close");
			}
		},
		close: function(event, ui)
		{
			$("#newBannerTypeID").val("");
			$("#newBannerURL").val("");
			$("#newBannerTarget").val("");
			$("#newBannerPriority").val("");
			$("#newBannerIsActive").attr("checked", true);
			$("#newBannerFilename").val("");
			$("#newBannerUpload").uploadifyClearQueue();
		}
	});
	
	$("#editBannerDialog").dialog({
		autoOpen: false,
		width: 570,
		resizable: false,
		buttons: {
			"Cập nhật": function(){
				$("#editBannerErrorRow").css("display", "none");
				$.post("/banner/edit", $("#editBannerForm").serialize(), function(data){
					var returnData = $.parseJSON(data);
					if (returnData.code == 0)
					{
						window.location.reload();
						$("#editBannerDialog").dialog("close");
					}
					else
					{
						$("#editBannerErrorRow").css("display", "");
						$("#editBannerErrorMessage").html(returnData.data);
					}
				});
				return false;
			},
			"Đóng": function(){
				$(this).dialog("close");
			}
		},
		close: function(event, ui)
		{
			$("#editBannerFilename").val("");
			$("#editBannerUpload").uploadifyClearQueue();
		}
	});
	
	$("#removeBannerDialog").dialog({
		autoOpen: false,
		width: 220,
		resizable: false,
		buttons: {
			"Xóa": function(){
				$.post("/banner/remove", $("#removeBannerForm").serialize(), function(data){
					var returnData = $.parseJSON(data);
					if (returnData.code == 0)
					{
						window.location.reload();
						$("#removeBannerDialog").dialog("close");
					}
				});
				return false;
			},
			"Đóng": function(){
				$(this).dialog("close");
			}
		}
	});
	
	$(".bannerHelpIcon").qtip({
		content: "<strong>Sách mới, Phim mới, Tem mới, Tạp chí mới</strong>: banner xuất hiện tại trang chủ của các chuyên mục tương ứng, kích thước 540px * 200px.",
   	show: "mouseover",
   	hide: "mouseout",
		style: {
			name: "cream",
			border: {
				width: 2,
				radius: 3
			},
			tip: 'bottomLeft'
		},
		position: {
			corner: {
				target: "topRight",
        tooltip: "bottomLeft"
			}
		}
	});
	
	$("#newBannerUpload").uploadify({
    "swf": "/images/uploadify/uploadify.swf",
    "uploader": "/banner/bannerimage",
		"buttonText": "+",
    "cancelImage": "/images/uploadify/uploadify-cancel.png",
    "auto" : true,
		"fileObjName": "newBannerImageUpload",
		"fileTypeExts": "*.jpg;*.gif;*.png",
  	"fileTypeDesc": "Image files",
		"height": 21,
		"width": 21,
		"removeCompleted": false,
		"onUploadSuccess": function(file, data, response){
			var returnData = $.parseJSON(data);
			if (returnData.code < 0)
			{
				$("#newBannerErrorRow").css("display", "");
				$("#newBannerErrorMessage").html(returnData.data);
				$("#newBannerFilename").val(-1);			
			}
			else
			{
				$("#newBannerErrorRow").css("display", "none");
				$("#newBannerErrorMessage").html("");
				$("#newBannerFilename").val(returnData.data);
			}
    }
  });
	
	$("#editBannerUpload").uploadify({
    "uploader": "/images/uploadify/uploadify2.swf",
    "script": "/upload/banner",
		"buttonImg": "/images/uploadify/upload_button.gif",
    "cancelImg": "/images/uploadify/cancel.png",
    "auto" : true,
		"fileDataName": "bannerUpload",
		"expressInstall": "/images/uploadify/expressInstall.swf",
		"fileExt": "*.jpg;*.gif;*.png;*.swf",
  	"fileDesc": "Image, flash files (.JPG, .GIF, .PNG, .SWF)",
		"height": 24,
		"width": 108,
		"removeCompleted": false,
		"onComplete": function(event, ID, fileObj, response, data){
			var returnData = $.parseJSON(response);
			if (returnData.code < 0)
			{
				$("#editBannerErrorRow").css("display", "");
				$("#editBannerErrorMessage").html(returnData.data);
				$("#editBannerFilename").val(-1);
			}
			else
			{
				$("#editBannerErrorRow").css("display", "none");
				$("#editBannerErrorMessage").html("");
      	$("#editBannerFilename").val(returnData.data);
			}
    }
  });
	
	$("#newBannerButton").click(function(){
		newBanner();
	});
	
	initControl();
	loadBannerType();
	loadBanner();
	
	/////////////////////////////////////////
	function initControl()
	{
		$("#newBannerButton").button();
	}
	
	// Load banner type from database
	function loadBannerType()
	{
		$.get("/banner/listbannertype", {}, function(data){
			var jsonData = $.parseJSON(data);
			if (jsonData.code == 0)
			{
				var htmlOptions = "";
				for (var i = 0; i < jsonData.data.length; i++)
					htmlOptions += "<option value='" + jsonData.data[i].ID + "'>" + jsonData.data[i].NAME + "</option>";
				$(".bannerTypeIDs").html(htmlOptions);
			}
		});
	}
	
	function loadBanner()
	{
		var typeID = $("#listBannerTypeID").val();
		if (typeID > 0)
		{
			$.get("/banner/list", {id: typeID}, function(data){
				var returnData = $.parseJSON(data);
				var bannerTable = document.getElementById("listBannerTable");
				while (bannerTable.rows.length > 1)
					bannerTable.deleteRow(bannerTable.rows.length - 1);
				for (var i = 0; i < returnData.data.length; i++)
				{
					var row = bannerTable.insertRow(bannerTable.rows.length);
					var cellHTML = "";
					cellHTML += "<td align='center'>" + returnData.data[i].ID + "</td>";
					if (returnData.data[i].MEDIA_TYPE == "image")
						cellHTML += "<td><img src='" + returnData.data[i].MEDIA + "' /></td>";
					else
						cellHTML += "<td>" + "&nbsp;" + "</td>";
					cellHTML += "<td>" + returnData.data[i].URL + "</td>";
					cellHTML += "<td align='center'>" + returnData.data[i].TARGET + "</td>";
					cellHTML += "<td align='center'>" + returnData.data[i].PRIORITY + "</td>";
					cellHTML += "<td align='center'>" + returnData.data[i].IS_ACTIVE + "</td>";
					cellHTML += "<td align='center'><a href='#'><img id='e" + returnData.data[i].ID + "' src='/images/icon/edit_32.png' class='editBanner' border='0' alt='Sửa' /></a> <a href='#'><img id='r" + returnData.data[i].ID + "' src='/images/icon/delete_32.png' class='removeBanner' border='0' alt='Xóa' /></a></td>";
					row.innerHTML = cellHTML;
				}
				$(".editBanner").click(function(){
					var bannerID = $(this).attr("id");
					editBanner(bannerID);
					return false;
				});
				$(".removeBanner").click(function(){
					var bannerID = $(this).attr("id");
					removeBanner(bannerID);
					return false;
				});
				$(".editBanner").qtip({
					content: "Sửa banner",
					show: {when: {event: "mouseover"}},
					hide: {when: {event: "mouseout"}},
					style: {
						name: "cream",
						border: {
							width: 2,
							radius: 3
						}
					},
					position: {
						corner: {
							target: 'leftMiddle',
            	tooltip: 'rightMiddle'
						}
					}
				});
				$(".removeBanner").qtip({
					content: "Xóa banner",
					show: {when: {event: "mouseover"}},
					hide: {when: {event: "mouseout"}},
					style: {
						name: "cream",
						border: {
							width: 2,
							radius: 3
						}
					},
					position: {
						corner: {
							target: 'leftMiddle',
            	tooltip: 'rightMiddle'
						}
					}
				});
			});
		}
	}
	
	function newBanner()
	{
		$("#newBannerErrorRow").css("display", "none");
		$("#newBannerDialog").dialog("open");
	}
	
	// Submit new banner form
	function newBannerSubmit()
	{
		$("#newBannerErrorRow").css("display", "none");
		$.post("/banner/add", $("#newBannerForm").serialize(), function(data){
			var returnData = $.parseJSON(data);
			if (returnData.code == 0)
			{
				loadBanner();
				$("#newBannerDialog").dialog("close");
			}
			else
			{
				$("#newBannerErrorRow").css("display", "");
				$("#newBannerErrorMessage").html(returnData.data);
			}
		});
	}
	
	function editBanner(bannerID)
	{
		bannerID = parseInt(bannerID.substr(1));
		if (bannerID > 0)
		{
			$.get("/banner/getinfo", {id: bannerID}, function(data){
				var returnData = $.parseJSON(data);
				if (returnData.code == 0)
				{
					$("#editBannerErrorRow").css("display", "none");
					$("#editBannerTypeID").val(returnData.data.BANNER_TYPE_ID);
					$("#editBannerURL").val(returnData.data.URL);
					$("#editBannerTarget").val(returnData.data.TARGET);
					$("#editBannerPriority").val(returnData.data.PRIORITY);
					if (returnData.data.IS_ACTIVE == 1)
						$("#editBannerIsActive").attr("checked", true);
					else
						$("#editBannerIsActive").attr("checked", false);
					$("#editBannerID").val(returnData.data.ID);
					$("#editBannerOldFilename").val(returnData.data.MEDIA);
					$("#editBannerDialog").dialog("open");
				}
			});
		}
	}
	
	function removeBanner(bannerID)
	{
		bannerID = parseInt(bannerID.substr(1));
		if (bannerID > 0)
		{
			$("#removeShowBannerID").html(bannerID);
			$("#removeBannerID").val(bannerID);
			$("#removeBannerDialog").dialog("open");
		}
	}
});